import VueRouter from "vue-router";
import Vue from "vue";

//重写push和replace方法
//记录当前push
let push = VueRouter.prototype.push
let replace = VueRouter.prototype.replace
//后者覆盖前者
VueRouter.prototype.push = function (location, success, defeated) {
    //如果传入了成功以及失败回调就用传入的
    if (success && defeated) {
        // 使用call修改this指向为当前VueRouter实例
        push.call(this, location, success, defeated)
    } else {
        // 如果没传就设置默认的
        push.call(this, location, () => { }, () => { })
    }
}
// 重写replace
VueRouter.prototype.replace = function (location, success, defeated) {
    //如果传入了成功以及失败回调就用传入的
    if (success && defeated) {
        // 使用call修改this指向为当前VueRouter实例
        replace.call(this, location, success, defeated)
    } else {
        // 如果没传就设置默认的
        replace.call(this, location, () => { }, () => { })
    }
}


//调用插件
Vue.use(VueRouter)

import { routes } from './routes'

//创建router配置对象
const router = new VueRouter({
    //配置路由模式
    mode: 'hash',
    routes,
    // 滚动事件  return:滚动条位置
    scrollBehavior(to, from, savePosition) {
        //上一次页面的滚动条位置
        // 如果有值,就访问到上一次的记录位置
        if (savePosition) {
            return savePosition
        } else {
            // 如果没有值默认0 顶部
            return { y: 0 }
        }
    }

})


export default router